﻿using System;

namespace LM.Tela.Desktop.Formularios
{
    using LM.InterfacesFabricas.DTOs;
    using LM.InterfacesFabricas.Fabricas;
    using LM.InterfacesFabricas.Interfaces.Servicos;
    using System.Collections.Generic;
    using System.Globalization;

    public partial class frmMarca
    {
        public frmMarca()
        {
            InitializeComponent();
        }

        private void btnSalvar_Click( object sender, EventArgs e )
        {
            Salvar();
        }

        private void Salvar()
        {
            int codigo;
            int.TryParse( txtCodigo.Text, out codigo );
            var marca = new DtoMarca {Codigo = codigo, Descricao = txtDescricao.Text, Modelo = null};

            var servicoDeMarca = FabricaGenerica.Crie<IServicoDeMarca>();

            if ( !EmEdicao() )
                servicoDeMarca.Cadastre( marca );
            else
                servicoDeMarca.Atualize( marca );

            PrepareTelaParaNovoRegistro();
        }

        private void LimpeCamposEdicao()
        {
            txtCodigo.Text = string.Empty;
            txtDescricao.Text = string.Empty;
        }

        private void CarregueItensJaCadastrados()
        {
            grdMarcas.DataSource = FabricaGenerica.Crie<IServicoDeMarca>().ConsulteTodos();
        }

        private void frmMarca_Load( object sender, EventArgs e )
        {
            PrepareTelaParaNovoRegistro();
        }

        private void grdMarcas_CellDoubleClick( object sender, System.Windows.Forms.DataGridViewCellEventArgs e )
        {
            if ( e.RowIndex >= 0 )
            {
                var marca = ( ( IList<DtoMarca> )grdMarcas.DataSource )[e.RowIndex];

                txtCodigo.Text = marca.Codigo.ToString( CultureInfo.InvariantCulture );
                txtCodigo.Enabled = false;
                txtDescricao.Text = marca.Descricao;
            }
        }

        private void btnNovo_Click( object sender, EventArgs e )
        {
            PrepareTelaParaNovoRegistro();
        }

        private bool EmEdicao()
        {
            return !string.IsNullOrWhiteSpace( txtCodigo.Text ) && txtCodigo.Enabled == false;
        }

        private void btnRemover_Click( object sender, EventArgs e )
        {
            if ( !EmEdicao() )
            {
                this.ExibaInconsistencia( "Marca não selecionada para edição." );
            }
            else
            {
                var servicoDeMarca = FabricaGenerica.Crie<IServicoDeMarca>();
                servicoDeMarca.Exclua( int.Parse( txtCodigo.Text ) );

                PrepareTelaParaNovoRegistro();
            }
        }

        private void PrepareTelaParaNovoRegistro()
        {
            LimpeCamposEdicao();
            CarregueItensJaCadastrados();
            txtCodigo.Enabled = true;
            txtCodigo.Focus();
        }

        private void grdMarcas_CellContentClick(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
        {

        }
    }
}
